#include <Gal.h>
#include <stdio.h>
#include <stdlib.h>
#include <xf86drm.h>
#include <xf86drmMode.h>
#include <nouveau_bo.h>
#include <nouveau_device.h>
#include <nouveau_channel.h>
#include <nouveau_drmif.h>
#include "Private.h"
#include <fcntl.h>

/// <summary>
/// TODO
/// </summary>
int _DriverInit(DriverPtr drv)
{
	int drm = -1;
	struct nouveau_device *device;

	if (drv == NULL)
		return -1;

	if ((drm = open(GAL_DRM_PATH, O_RDWR)) < 0)
		return -2;

	if (drmSetMaster(drm))
		return -3;

	if (nouveau_device_open_existing(&device, 0, drm, 0) == 0)
		return -4;

	if (NVInitDma(drv) < 0)
		return -4;

	if (NVInitDisplays(drv, drm) < 0)
		return -4;

	printf("Chipset: 0x%x\n", (device->chipset & 0xf0));
	//drv->state = 0;
	//drv->name = "Generic FBDev";
	//drv->Open = FBDOpen;
	//drv->CreateSurface = FBDCreateSurface;
	//drv->DestroySurface = FBDDestroySurface;
	//drv->CreateFramebuffer = FBDCreateFramebuffer;
	//drv->Close = FBDClose;
	drv->private = (void*)device;
	drv->handle = drm;

	return 0;
}

/// <summary>
/// TODO
/// </summary>
int NVInitPrivate(DriverPtr drv)
{
	if (drv == NULL)
		return -1;

	if (!(drv->private = malloc(sizeof(NV50Private))))
		return -1;

	return 0;
}

/// <summary>
/// TODO
/// </summary>
int _DriverExit(DriverPtr drv)
{
	if (drv == NULL)
		return -1;

	return 0;
}
